package com.zrx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.zrx.domain.ResponseResult;
import com.zrx.domain.dto.ChangeStatusDto;
import com.zrx.domain.dto.EditTagDto;
import com.zrx.domain.dto.TagListDto;
import com.zrx.domain.entity.Category;
import com.zrx.domain.entity.Role;
import com.zrx.domain.entity.Tag;
import com.zrx.domain.vo.ExcelCategoryVo;
import com.zrx.domain.vo.PageVo;
import com.zrx.enums.AppHttpCodeEnum;
import com.zrx.service.CategoryService;
import com.zrx.utils.BeanCopyUtils;
import com.zrx.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类
 * @author 张荣欣
 * @create ：2022/10/8  14:48
 */
@RestController
@RequestMapping("/content/category")
@Api(tags = "分类")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/listAllCategory")
    @ApiOperation(value = "查询所有分类",notes = "查询所有分类")
    public ResponseResult listAllCategory(){
        return categoryService.listAllCategory();
    }

    /**
     * 查询分类 分页
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有分类 分页",notes = "查询所有分类 分页")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize,Category category){
        return categoryService.pageCategoryList(pageNum,pageSize,category);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "刪除分类", notes = "刪除分类")
    public ResponseResult deleteCategory(@PathVariable("id") List<Long> ids){
        categoryService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取分类", notes = "根据id获取分类")
    public ResponseResult getByIdOneCategory(@PathVariable("id") Long id){

        return ResponseResult.okResult(categoryService.getById(id));
    }
    @PutMapping()
    @ApiOperation(value = "修改分类", notes = "修改分类")
    public ResponseResult updateCategory(@RequestBody Category category){
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }

    @PostMapping()
    @ApiOperation(value = "新增标签", notes = "新增标签")
    public ResponseResult addCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseResult.okResult();
    }

    /**
     * 更改状态
     * @param statusDto
     * @return
     */
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody ChangeStatusDto statusDto){
        Category category = new Category();
        category.setId(statusDto.getId());
        category.setStatus(statusDto.getStatus());
        return ResponseResult.okResult(categoryService.updateById(category));
    }

    @ApiOperation(value = "分类导出",notes = "分类导出")
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();

            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }


}
